<template>
  <div class="index-container">
    <el-row :gutter="20">
      <el-col
        v-for="(item, index) in iconList"
        :key="index"
        :lg="6"
        :md="6"
        :sm="6"
        :xl="6"
        :xs="12"
      >
        <el-card class="icon-panel" shadow="hover">
          <div
            @click="DeviceStateTypeData(item.title)"
            style="
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: center;
            "
          >
            <el-badge :value="item.value">
              <vab-icon
                :icon="item.icon"
                :style="{ color: item.color, fontSize: '40px' }"
              />
            </el-badge>
            <p>{{ item.title }}</p>
          </div>
        </el-card>
      </el-col>

      <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
        <el-card class="access" shadow="hover" skeleton>
          <template #header>
            <vab-icon icon="archive-drawer-line" />
            设备状态监控
          </template>
          <div class="bottom">
            <el-row :gutter="20">
              <el-col
                v-for="(item, index) in deviceStateData"
                :key="index"
                :lg="6"
                :md="6"
                :sm="6"
                :xl="6"
                :xs="6"
              >
                <equipment-state :DeviceInfo="item" />
              </el-col>
            </el-row>
          </div>
          <el-pagination
            background
            :current-page="queryForm1.pageNo"
            :layout="layout"
            :page-size="queryForm1.pageSize"
            :total="total1"
            @current-change="handleCurrentChange1"
            @size-change="handleSizeChange1"
          />
        </el-card>
      </el-col>

      <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
        <Tabs />
      </el-col>
    </el-row>

    <el-dialog :close-on-click-modal="false" 
      :visible="dialogVisible"
      :title="title"
      width="60%"
      :before-close="handleClose"
    >
      <div>
        <el-table :data="tableData" height="500" style="width: 100%">
          <el-table-column label="设备状态" prop="DeviceState">
            <template #default="scope">
              <el-tag class="ml-2" type="success">良好</el-tag>
            </template>
          </el-table-column>
          <el-table-column label=" 设备编号" prop="DeviceId" />
          <el-table-column label="设备名称" prop="DeviceName" />
          <el-table-column label="设备类型" prop="DeviceType" />
          <el-table-column label="保养内容" prop="DeviceContent" />
          <template #empty>
            <el-empty class="vab-data-empty" description="暂无数据" />
          </template>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogVisible = false">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {
    List,
    DeviceState,
    DeviceStatistics,
    DeviceRunningInfo,
  } from '@/api/index'
  import EquipmentState from '@/components/equipment/state'
  import Tabs from './components/Tabs'

  export default {
    name: 'Index',
    components: {
      EquipmentState,
      Tabs,
    },
    data() {
      return {
        iconList: [
          {
            icon: 'picture-in-picture-2-line',
            title: '通讯中',
            link: '',
            color: '#95de64',
            value: 0,
          },
          {
            icon: 'picture-in-picture-exit-line',
            title: '运行中',
            link: '',
            color: '#69c0ff',
            value: 0,
          },
          {
            icon: 'picture-in-picture-line',
            title: '待保养',
            link: '',
            color: '#ffd666',
          },
          {
            icon: 'pixelfed-line',
            title: '故障中',
            link: '/other/player',
            color: '#ffc069',
          },
        ],
        deviceStateData: [],
        tableData: [],
        dialogVisible: false,

        queryForm: {
          pageNo: 1,
          pageSize: 10,
        },
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        title: '',

        queryForm1: {
          pageNo: 1,
          pageSize: 20,
        },
        total1: 0,
      }
    },
    created() {
      this.initData()
    },
    methods: {
      handleClose() {
        this.dialogVisible = false
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },

      handleCurrentChange1(val) {
        this.queryForm1.pageNo = val
        this.getDeviceStateData()
      },
      handleSizeChange1(val) {
        this.queryForm1.pageSize = val
        this.getDeviceStateData()
      },
      DeviceStateTypeData(title) {
        this.tableData = []
        this.title = title
        this.fetchData()
      },

      fetchData() {
        if (this.title == '通讯中' || this.title == '运行中') {
          const params = {
            Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
            Count: this.queryForm.pageSize,
            Type: this.title,
          }
          DeviceRunningInfo(params).then((res) => {
            this.dialogVisible = true
            this.total = res.TotalCount
            this.tableData = res.MaintenanceInfo
          })
        } else if (this.title == '待保养' || this.title == '故障中') {
          const params = {
            Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
            Count: this.queryForm.pageSize,
            Type: this.title,
          }
          List(params).then((res) => {
            this.tableData = res.MaintenanceInfo
            this.total = res.TotalCount
            this.dialogVisible = true
          })
        }
      },
      getDeviceStateData() {
        const params = {
          Index: this.getPage(this.queryForm1.pageNo, this.queryForm1.pageSize),
          Count: this.queryForm1.pageSize,
        }
        DeviceState(params).then((res) => {
          console.log('DeviceTotalCount', res.DeviceTotalCount)
          this.deviceStateData = res.DeviceInfo
          this.total1 = res.DeviceTotalCount
        })
      },
      initData() {
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
        }
        List(params).then((res) => {
          console.log(res)
        })
        this.getDeviceStateData()
        DeviceStatistics({}).then((res) => {
          this.iconList[0]['value'] = res.CommunicateDeviceCount
          this.iconList[1]['value'] = res.RunningDeviceCount
          this.iconList[2]['value'] = res.MaintaineDeviceCount
          this.iconList[3]['value'] = res.FaultDeviceCount
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .index-container {
    padding: 0 !important;
    background: $base-color-background !important;

    :deep() {
      .access,
      .authorization,
      .version-information {
        min-height: 268px;
      }

      .el-card {
        .el-card__header {
          position: relative;

          .card-header-tag {
            position: absolute;
            top: 15px;
            right: $base-margin;
          }

          > div > span {
            display: flex;
            align-items: center;

            i {
              margin-right: 3px;
            }
          }
        }

        .el-card__body {
          position: relative;

          .echarts {
            width: 100%;
            height: 127px;
          }

          .card-footer-tag {
            position: absolute;
            right: $base-margin;
            bottom: 15px;
          }
        }
      }

      .bottom {
        padding-top: 20px;
        margin-top: 5px;
        color: #595959;
        text-align: left;
        border-top: 1px solid $base-border-color;
      }
    }
  }
</style>
